package com.wjy.demo.error;


import com.wjy.demo.vo.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {


    /**
     * 处理自定义的业务异常
     */
    @ResponseBody
    @ExceptionHandler(value = ServiceException.class)
    public Response bizExceptionHandler(HttpServletRequest req, ServiceException e) {
        log.error("发生业务异常！原因是: {}", e.getMessage());
        return Response.fail(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response exceptionHandler(HttpServletRequest req, Exception e) {
        log.error("系统异常！原因是: {}", e);
        return Response.fail(ErrorType.SERVER_SEEOR);
    }



}
